/****************************************************************************
 * Copyright (C) 2010
 * by Dimok
 * Modified by Dj_Skual
 * for SaveGame Manager GX
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 ***************************************************************************/
#include "UpdateSettingsMenu.hpp"
#include "../../Controls/Application.h"
#include "../../Prompts/PromptWindows.h"
#include "../../Prompts/ProgressWindow.h"
#include "../../Network/Updates/Update.h"
#include "../../Network/Updates/UpdateIconTask.h"
#include "../../Network/Updates/CheckForUpdateTask.h"
#include "../../Network/svnrev.h"
#include "../../Network/ChangeLog.h"
#include "../../System/sys.h"

static const char * OnOffText[MAX_ON_OFF] =
{
	trNOOP( "OFF" ),
	trNOOP( "ON" )
};

static const char *LanguageText[LANG_UPDATE_MAX] =
{
	trNOOP( "OFF" ),
	trNOOP( "All" ),
	trNOOP( "Installed" )
};

UpdateSettingsMenu::UpdateSettingsMenu()
	: SettingsBrowser()
{
	SetupOptionName();
	show();
}

void UpdateSettingsMenu::SetupOptionName()
{
	int Idx = 0;
	options->SetName(Idx++, tr( "Auto Connect" ));
	options->SetName(Idx++, tr( "Icon Update" ));
	options->SetName(Idx++, tr( "Language Update" ));
	options->SetName(Idx++, tr( "GameTDB Update" ));
	options->SetName(Idx++, tr( "Update" ));
	
	SetOptionValues();
}

void UpdateSettingsMenu::SetOptionValues()
{
	int Idx = 0;

	//! Settings: Auto Update
	options->SetValue(Idx++, tr(OnOffText[Settings.AutoConnect]));
	
	//! Settings: Icon Update
	if (Settings.UpdateIcon == 0)
		options->SetValue(Idx++, tr("OFF"));
	else if (Settings.UpdateIcon == 1)
		options->SetValue(Idx++, tr("Standard"));
	else
		options->SetValue(Idx++, "Black.Pearl");
	
	//! Settings: Language Update
	options->SetValue(Idx++, tr(LanguageText[Settings.UpdateLanguage]));
	
	//! Settings: GameTDB Update
	options->SetValue(Idx++, tr(OnOffText[Settings.UpdateGameTDB]));
	
	//! Button: Update
	options->SetValue(Idx++, " ");
}

void UpdateSettingsMenu::OnBrowserClick(GuiOptionBrowser * browser UNUSED, int option)
{
	switch (option)
	{
		case 0:
			if (++Settings.AutoConnect >= MAX_ON_OFF) Settings.AutoConnect = OFF;
			break;
		case 1:
			if (++Settings.UpdateIcon > 2) Settings.UpdateIcon = OFF;
			
			//! download new icon
			if(Settings.UpdateIcon)
			{
				std::string text = tr("Standard Icon");
				if(Settings.UpdateIcon == 2)
					text = tr("Black.Pearl Icon");
				
				if(WindowPrompt(text.c_str(), tr("Do you want to download now?"), tr("Yes"), tr("Cancel")))
				{
					UpdateIconTask * task = new UpdateIconTask();
					
					while(task->IsRunning())
						Application::Instance()->updateEvents();
					
					StopProgress();
					
					if(task->Succes())
						ShowMsg(fmt(tr("%s successfully updated."), "icon.png"), 0);
					else
						ShowError(tr("%s update failed."), "icon.png");
					
					delete task;
				}
			}
			break;
		case 2:
			if (++Settings.UpdateLanguage >= LANG_UPDATE_MAX)
				Settings.UpdateLanguage = LANG_UPDATE_OFF;
			break;
		case 3:
			if (++Settings.UpdateGameTDB >= MAX_ON_OFF) Settings.UpdateGameTDB = OFF;
			break;
		case 4:
			{
				CheckForUpdateTask * checkTask = new CheckForUpdateTask();
				
				while(checkTask->IsRunning())
					Application::Instance()->updateEvents();
				
				int newRev = checkTask->GetRev();
				std::string downloadUrl = checkTask->GetUrl();
				
				delete checkTask;
				
				if (newRev > GetIntRev())
				{
					int choice = 1;
					do
					{
						choice = WindowPrompt(fmt(tr("Update to Rev%i available"), newRev), tr("Do you want to update now ?"), (Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_UPDATES)) ? tr("Yes") : NULL, tr("Show Changelog"), tr("Cancel"));
						
						if(choice == 1 && (!Settings.GodMode && (Settings.ParentalBlocks & BLOCK_UPDATES)))
							choice = 2;
						
						if(choice == 1)
						{
							if(UpdateApp(downloadUrl.c_str()))
							{
								if(WindowPrompt(tr("Update successfully finished"), tr("Do you want to reboot now ?"), tr("Yes"), tr("No")))
									RebootApp();
							}
							break;
						}
						else if (choice == 2)
						{
							ChangeLog Changelog;
							if(!Changelog.Show())
								ShowError(tr("Failed to get the Changelog."));
						}
						
					} while(choice);
				}
				else
				{
					int choice = 1;
					do
					{
						choice = WindowPrompt(tr("No new updates available"), "", (Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_UPDATES)) ? tr("Update files") : "", tr("Show Changelog"), tr("Cancel"));
						
						if(choice == 1 && (!Settings.GodMode && (Settings.ParentalBlocks & BLOCK_UPDATES)))
							choice = 2;
						
						if(choice == 1)
						{
							if(UpdateFiles())
								ShowMsg(tr("Update successfully finished"), 0);
							
							break;
						}
						else if(choice == 2)
						{
							ChangeLog Changelog;
							if(!Changelog.Show())
								ShowError(tr("Failed to get the Changelog."));
						}
						
					} while(choice);
				}
			}
			break;
		default:
			break;
	}

	SetOptionValues();
	optionBrowser->Refresh();
}
